package com.ttt.gdfood.controller;

import com.ttt.gdfood.entity.Comment;
import com.ttt.gdfood.entity.Note;
import com.ttt.gdfood.service.CommentService;
import com.ttt.gdfood.util.JsonResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comment")
@CrossOrigin
public class CommentController {
    @Autowired
    CommentService commentService;

    @GetMapping("/getAll")
    List<Comment> getAll(@RequestParam("note_id") int note_id){
        return commentService.getAllComment(note_id);
    }

    @PostMapping("/addComment")
    public JsonResult<Void> addComment(@RequestBody Comment comment) {
        // 创建返回值
        JsonResult<Void> result = new JsonResult<Void>();
        try {
// 调用业务对象执行添加
            commentService.addComment(comment);
            // 响应成功
            result.setState(200);
            result.setMessage("发表成功");
        } catch (Exception e) {
// 添加异常
            result.setState(500);
            result.setMessage("发表失败");
        }
        return result;
    }
}
